import Storage from '@/common/storage.js';
const query = Storage.getQuery() || {};
const addr = Storage.getAddr() || {}

const state = {
	orderDetail: null,
	device: {},
	time: {
		day: 0,
		hour: 0,
		minute: 0,
		seconde: 0
	},
	addressObj: addr,
	query: query,
	redPack: 200000
}
const mutations = {
	setTime(state, data) {
		state.time = data
	},
	setOrderDetail(state, data) {
		Storage.setOrderDetail(data)
		state.orderDetail = data;
	},
	setQuery(state, data) {
		state.query = data;
		Storage.setQuery(data)
	},
	setAddress(state, data) {
		Storage.setAddr(data)
		state.addressObj = data;
	},
	setDevice(state, data) {
		state.device = data
	},
	setUserInfo(state, data) {
		Storage.setUse(data)
		state.userInfo = data
	}
}
const actions = {
	getTime(state) {
		var date = new Date();
		var y = date.getFullYear();
		var m = date.getMonth();
		var d = date.getDate() + 1;
		var time = new Date(y, m, d, 0, 0, 0);
		var num = time.getTime() - date.getTime();
		var day = parseInt(num / (24 * 60 * 60 * 1000));
		num = num % (24 * 60 * 60 * 1000);
		var hour = parseInt(num / (60 * 60 * 1000));
		num = num % (60 * 60 * 1000);
		var minute = parseInt(num / (60 * 1000));
		num = num % (60 * 1000);
		var seconde = parseInt(num / 1000);
		// hour = hour < 10 ? '0' + hour : hour;
		// minute = minute < 10 ? '0' + minute : minute;
		// seconde = seconde < 10 ? '0' + seconde : seconde;
		// var timeStr = hour + ":" + minute + ":" + seconde;
		state.commit('setTime', {
			day,
			hour,
			minute,
			seconde
		})
	}
}
const getters = {
	click_id(state) {
		let click_id = click_id || "";
		switch (state.query.from) {
			case 'mammut':
				if (state.query.click_id) click_id = state.query.click_id;
				break;
			case 'toutiao':
				if (state.query.clickid) click_id = state.query.clickid;
				break;
			case 'qtt':
				if (state.query.callback_url) click_id = state.query.callback_url;
				break;
			case 'jiguang':
				if (state.query.jcid) click_id = state.query.jcid;
				break;
			case 'tuia':
				if (state.query.a_tuiaId) click_id = state.query.a_tuiaId;
				break;
			case 'cat':
				if (state.query.bxmid) click_id = state.query.bxmid;
				break;
			case 'guangqu':
				if (state.query.ccbid) click_id = state.query.ccbid;
				break;
			case 'huawei':
			case 'way':
				if (state.query.callback) click_id = encodeURIComponent(state.query.callback);
				break;
			default:
				if (state.query.gdt_vid) click_id = state.query.gdt_vid;
				break;
		}
		Storage.setClkId(click_id)
		return click_id
	},
	isWX(state) {
		let {
			ua
		} = state.device;
		let isWXWork = ua.toLowerCase().match(/wxwork/i) == 'wxwork'
		let isWeixin = !isWXWork && ua.toLowerCase().match(/micromessenger/i) == 'micromessenger';
		return isWeixin
	},
	getOrderDetail(state) {
		if (state.orderDetail) {
			return state.orderDetail
		} else {
			return Storage.getOrderDetail();
		}
	}
}

export default {
	state,
	mutations,
	actions,
	getters
}